/*
 * JClipsExample.java
 *
 * Created on 17 de julio de 2007, 15:07
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package mike;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import javax.servlet.jsp.JspWriter;
import jclips.JClips;

/**
 *
 * @author portatil
 */
public class JClipsExample implements Observer {

    private static final String KNOWLEDGE_FILE = "myknowledge.clp";
    JspWriter out;
    
    public JClipsExample(JspWriter out){
        this.out = out;
    }
    
    public void update(Observable jClips, Object messageObj) {
        String message = (String) messageObj;
        try {
            out.println("Message received: " + message);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void doIt(JspWriter out) {
     /*   try {
            out.print(JClips.getVersion());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
*/
        JClips jClips = JClips.getInstance();
        jClips.init();
        jClips.addObserver(new JClipsExample(out));

        try {
            URL fileUrl = JClipsExample.class.getResource(KNOWLEDGE_FILE);
            if (fileUrl == null) {
                throw new FileNotFoundException(KNOWLEDGE_FILE);
            }
            File file = new File(new URI(fileUrl.toString()));
            jClips.load(file.getPath());
            jClips.reset();
            jClips.run();
            jClips.executeCommand("(assert (do-rest))");
            jClips.run();
            //jClips.commandLoop();
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }

        jClips.deallocate();
    }
    
 

}